package org.boulderfoodrescue.webapp.client.managers;

import java.util.List;

import org.boulderfoodrescue.webapp.client.events.ChangeType;
import org.boulderfoodrescue.webapp.client.events.PickupReportUpdatedEvent;
import org.boulderfoodrescue.webapp.client.events.PickupReportUpdatedHandler;
import org.boulderfoodrescue.webapp.client.rpc.PickupReportServiceAsync;
import org.boulderfoodrescue.webapp.client.rpc.RPCCallCompleted;
import org.boulderfoodrescue.webapp.shared.PickupReport;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class PickupReportManager {
	private static PickupReportServiceAsync pickupReportRPC;
	private static EventBus eventBus;
	private static List<PickupReport> pickups;

	public PickupReportManager(PickupReportServiceAsync pickupReportRPC, EventBus eventBus)
	{
		PickupReportManager.pickupReportRPC = pickupReportRPC;
		PickupReportManager.eventBus = eventBus;
		bind();
	}

	private void bind()
	{
		eventBus.addHandler(PickupReportUpdatedEvent.getType(), 
				new PickupReportUpdatedHandler(){
			@Override
			public void onPickupReportAdded(
					PickupReportUpdatedEvent event) {
				pickups.add(event.getValue());
			}
			@Override
			public void onPickupReportUpdated(
					PickupReportUpdatedEvent event) {
			}
			@Override
			public void onPickupReportRemoved(
					PickupReportUpdatedEvent event) {
				pickups.remove(event.getValue());
			}
		});
	}

	public static List<PickupReport> getPickupReports(final RPCCallCompleted callback){
		if(pickups != null)
			return pickups;
		pickupReportRPC.getAllPcikupReports(new AsyncCallback<List<PickupReport>>(){
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("ERROR: Failed to retrieve pickup reports from server."+caught.getMessage());
				Log.warn(caught.getMessage());
				callback.onRpcFailed();
			}

			@Override
			public void onSuccess(List<PickupReport> result) {
				if(result == null){
					Window.alert("Coulder not load pickup reports");
					Log.warn("Could not load pickup reports from server");
					callback.onRpcFailed();
				}
				else{
					callback.onRpcCompleted();
					for(PickupReport pr : result){
						eventBus.fireEvent(new PickupReportUpdatedEvent(ChangeType.Added, pr));
					}
				}
			}
		});
		return null;
	}

	public static void savePickup(final PickupReport report, final RPCCallCompleted callback) {
		pickupReportRPC.savePickupReport(report, new AsyncCallback<Long>(){

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Coulder not save pickup reports");
				Log.warn("Coulder not save pickup reports to server");
				callback.onRpcFailed();
			}

			@Override
			public void onSuccess(Long result) {
				if(result == null){
					Window.alert("Coulder not save pickup reports");
					Log.warn("Coulder not save pickup reports to server");
					callback.onRpcFailed();
				}
				else{
					callback.onRpcCompleted();
					report.setDatabaseKey(result);
					eventBus.fireEvent(new PickupReportUpdatedEvent(ChangeType.Added, report));
				}
			}

		});
	}
}
